package com.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Collect;
import com.entity.Menu;
import com.entity.Product;
import com.entity.ProductCategory;
import com.entity.User;
import com.service.collect.impl.CollectServiceImpl;
import com.service.layui.impl.LayuiServiceImpl;
import com.service.menurole.MenuServiceImpl;
import com.service.product.impl.ProductServiceImpl;
import com.servlet.BaseServlet;
import com.utils.EmptyUtils;
import com.utils.Pager;

@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/**
	 * 根据传递的id查找对应的商品信息进行显示
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryProductDeatil(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//显示三层分类
		List<ProductCategory> list1 = new ProductServiceImpl().finSelectProductCategory(1);
		List<ProductCategory> list2 = new ProductServiceImpl().finSelectProductCategory(2);
		List<ProductCategory> list3 = new ProductServiceImpl().finSelectProductCategory(3);
		//查询分类的结果存入request域中
		request.setAttribute("list", list1);
		request.setAttribute("list2", list2);
		request.setAttribute("list3", list3);
		
		//获取传递的商品id值
		int id = Integer.parseInt(request.getParameter("id"));
		//调用查询商品方法
		Product pro = new ProductServiceImpl().finBtnProduct(id);
		//判断商品是否存在,如果存在则跳转到商品详细页面,如果不存在,跳转到首页
		if(pro == null) {
			return "/pre/Index";
		}else {
			request.setAttribute("product", pro);
		}
		
		//收藏按钮显示
		//获取当前用户id
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("easybuyUserLogin");
		if(user != null) {
			List<Collect> list = new CollectServiceImpl().finselectByUserId(user.getId());
			if(list!=null) {
				for (Collect easybuyCollect : list) {
					if(pro.getId()==easybuyCollect.getProductId()) {
						request.setAttribute("easybuyCollect", easybuyCollect);
					}
				}
			}
		}
		
		return "/pre/product/productDeatil";
	}
	
	/**
	 * 根据分类查找商品
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//显示三层分类
		List<ProductCategory> list1 = new ProductServiceImpl().finSelectProductCategory(1);
		List<ProductCategory> list2 = new ProductServiceImpl().finSelectProductCategory(2);
		List<ProductCategory> list3 = new ProductServiceImpl().finSelectProductCategory(3);
		//查询分类的结果存入request域中
		request.setAttribute("list", list1);
		request.setAttribute("list2", list2);
		request.setAttribute("list3", list3);
		
		int category = Integer.parseInt(request.getParameter("category"));// id
		String menu = request.getParameter("menu");// id
		String currentPageStr = request.getParameter("currentPage");
		// 设置Pager
		String pageSizeStr = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSizeStr) ? 8 : Integer.parseInt(pageSizeStr);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		int count = new ProductServiceImpl().finPageCount(category);	//查询总数据
		Pager pager = new Pager(count, rowPerPage, currentPage);
		pager.setUrl("ProductServlet?action=queryProductList&category=" + category);
		List<Product> list = new ProductServiceImpl().finPageProduct(category, pager);
		//收藏按钮
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("easybuyUserLogin");
		if(user != null) {
			List<Collect> ulist =  new CollectServiceImpl().finselectByUserId(user.getId());
			request.setAttribute("listCollect", ulist);
		}
		//放置对象
		request.setAttribute("pager", pager);
		request.setAttribute("productList", list);
		request.setAttribute("menu", menu);
		request.setAttribute("total", count);
		return "/pre/product/queryProductList";
	}
	
	/**
	 * 模糊查询
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String queryLikeProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//显示三层分类
		List<ProductCategory> list1 = new ProductServiceImpl().finSelectProductCategory(1);
		List<ProductCategory> list2 = new ProductServiceImpl().finSelectProductCategory(2);
		List<ProductCategory> list3 = new ProductServiceImpl().finSelectProductCategory(3);
		//放置三层对象
		request.setAttribute("list", list1);
		request.setAttribute("list2", list2);
		request.setAttribute("list3", list3);
		
		String name = request.getParameter("keyWord");
		String menu = request.getParameter("menu");
		String currentPageStr = request.getParameter("currentPage");
		// 设置Pager
		String pageSizeStr = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSizeStr) ? 8 : Integer.parseInt(pageSizeStr);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		int count = new ProductServiceImpl().finLikeCount(name);	//查询总数据
		Pager pager = new Pager(count, rowPerPage, currentPage);	//pager对象
		pager.setUrl("ProductServlet?action=queryLikeProductList");
		List<Product> list = new ProductServiceImpl().finLikeProduct(name, pager);//调用查询方法
		//收藏按钮
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("easybuyUserLogin");
		if(user != null) {
			List<Collect> ulist =  new CollectServiceImpl().finselectByUserId(user.getId());
			request.setAttribute("listCollect", ulist);
		}
		//放置对象
		request.setAttribute("pager", pager);
		request.setAttribute("productList", list);
		request.setAttribute("menu", menu);
		request.setAttribute("total", count);
		return "/pre/product/queryProductList";
	}
	
	public String queryProductList4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//显示三层分类
		List<ProductCategory> list1 = new ProductServiceImpl().finSelectProductCategory(1);
		List<ProductCategory> list2 = new ProductServiceImpl().finSelectProductCategory(2);
		List<ProductCategory> list3 = new ProductServiceImpl().finSelectProductCategory(3);
		//放置三层对象
		request.setAttribute("list", list1);
		request.setAttribute("list2", list2);
		request.setAttribute("list3", list3);
		
		//获取登录用户的id
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("easybuyUserLogin");
		List<Collect> clist = new CollectServiceImpl().finselectByUserId(user.getId());
		
		// 设置Pager
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		int rowPerPage = EmptyUtils.isEmpty(pageSizeStr) ? 8 : Integer.parseInt(pageSizeStr);
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		int count = new CollectServiceImpl().finUserProCount(user.getId());	//查询总数据
		Pager pager = new Pager(count, rowPerPage, currentPage);	//pager对象
		pager.setUrl("ProductServlet?action=queryProductList4");
		//调用查询方法
		List<Product> plist = new ProductServiceImpl().CollectProduct(clist);
		//收藏按钮
		if(user != null) {
			List<Collect> ulist =  new CollectServiceImpl().finselectByUserId(user.getId());
			request.setAttribute("listCollect", ulist);
		}
		//放置对象 
		request.setAttribute("pager", pager);
		request.setAttribute("productList", plist);
		request.setAttribute("total", count);
		return "/pre/product/queryProductList";
	}
	
	public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取当前页数
		String currentPageStr = request.getParameter("currentPage");
		// 获取页大小
		String pageSize = request.getParameter("pageSize");
		//页面条数
		int rowPerPage = EmptyUtils.isEmpty(pageSize) ? 5 : Integer.parseInt(pageSize);
		//当前页
		int currentPage = EmptyUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		// 获取总记录数！
		int total = new ProductServiceImpl().finProCount();
		
		Pager pager = new Pager(total, rowPerPage, currentPage);
		if(pager.getPageCount()<pager.getCurrentPage()) {
			pager.setCurrentPage(currentPage-1);
		}
		pager.setUrl("/ProductServlet?action=index");
		// 访问三层！
		List<Product> productList = new LayuiServiceImpl().finProduct(pager);
		User user = (User)request.getSession().getAttribute("easybuyUserLogin");
		List<Menu> list = new MenuServiceImpl().allMenu(user.getId());
		request.getSession().setAttribute("litbioaList", list);
		// 放置对象！
		request.setAttribute("productList", productList);
		request.setAttribute("pager", pager);
		request.setAttribute("menu", 5);
		return "/backend/product/productList";
	}
	

	@Override
	public Class<?> getServletClass() {
		return ProductServlet.class;
	}

}
